//
//  ProgressView.m
//  GoldOperationClient
//
//  Created by yaxue wang on 16/8/11.
//  Copyright © 2016年 展鸿科技. All rights reserved.
//

#import "ProgressView.h"
#import "ProgressCell.h"
#import "ProgressModel.h"
#import "CBaseTable.h"
#import "ProgressCell.h"
#import "ProgressModel.h"
#import "MakePhoneView.h"
#define MINIMUM_ZOOM_ARC 0.014 //approximately 1 miles (1 degree of arc ~= 69 miles)
#define ANNOTATION_REGION_PAD_FACTOR 1.5
#define MAX_DEGREES_ARC 360
@interface ProgressView(){
    
    NSMutableDictionary *_dataDic;
    dispatch_source_t _timer;
}
@property (nonatomic, strong) NSString*orderUuid;

@end
@implementation ProgressView
-(instancetype)initWithFrame:(CGRect)frame withWarningUuid:(NSString *)warningUuid
{
    self = [super initWithFrame:frame];
    if (self) {
        _orderUuid=warningUuid;
        _dataDic =[[NSMutableDictionary alloc]init];
        //        [self setShowNoDataNotice:@"暂无报修单"];
        [self setShowNoDataNotice:@"没有进行中的报修单"];
        
        self.backgroundColor =[UIColor clearColor];
        self.separatorStyle=UITableViewCellSeparatorStyleNone;
        self.showsVerticalScrollIndicator =NO;
        [self registerCell:[ProgressCell class] withModel:[ProgressModel class]];
        //死的
        [self setDataWithUrl:@"warninglist_orderStateFlow" andParam:@{@"warningUuid":warningUuid}];
        [self setTableActionType:CTableActionType_Both];
        [self loadData];
        
        
        
    }
    return self;
    
}
- (void)reloadData
{
    //  NSInteger click = 0;
    
    for (int i=0; i<self.mainData.count; i++) {
        ProgressModel *model = [self.mainData objectAtIndex:i];
        NSInteger num =[model.P_state integerValue];
        //评价不显示
        if (num==9) {
            [self.mainData removeObjectAtIndex:i];
        }
        //        if ([model.P_state isEqualToString:@"10000"]) {
        //            click = 1;
        //        }
        //        if (num==2) {
        //            click++;
        //            if (click==1) {
        //
        //                model.P_state = @"10000";
        //
        //            }else{
        //                model.P_state = @"02";
        //            }
        //        }
        
        
    }
    
    [super reloadData];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.mainData.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ProgressModel *model = [self.mainData objectAtIndex:indexPath.row];
    NSInteger num=[model.P_state integerValue];
    CGFloat width=10+35+14;
    
    //分配 挂起   完成
    if(num==1||num==5||num==8)
        
    {
        CGSize labelsize = CGSizeZero;
        
        if ([model.P_content isKindOfClass:[NSNull class]]||[model.P_content isEqualToString:@""]) {
            
        }else
        {
            
            labelsize = [model.P_content sizeWithFont:F(12) constrainedToSize:CGSizeMake(WindowWidth-width, 900)];
            
        }
        return MAX((labelsize.height+10) + 65-14, 65);
        
    }else if(num==3)//转介 电话和备注
    {
        CGSize labelsize = CGSizeZero;
        
        if ([model.P_content isKindOfClass:[NSNull class]]||[model.P_content isEqualToString:@""]) {
            
        }else
        {
            
            labelsize = [model.P_content sizeWithFont:F(12) constrainedToSize:CGSizeMake(WindowWidth-width, 900)];
            
        }
        
        return MAX((labelsize.height+10) + 80-14, 80);
        
    }else
    {
        return  65;
        
    }
    
    
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    ProgressCell*cell = [tableView dequeueReusableCellWithIdentifier:@"cell1"]; //出列可重用的
    if (cell == nil) {
        cell = [[ProgressCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    cell.backgroundColor =[UIColor whiteColor];
    if (self.mainData.count==1) {
        cell.isRow=YES;
    }else
    {
        cell.isRow=NO;
        
    }
    
    ProgressModel *model1 = [self.mainData objectAtIndex:0];
    [cell setModel:model1];
    
    
    
    ProgressModel *model = [self.mainData objectAtIndex:indexPath.row];
    [cell setModel:model];
    if (indexPath.row==self.mainData.count-1) {
        cell.lineImg.hidden=YES;
    }else
    {
        cell.lineImg.hidden=NO;
        
    }
    if(indexPath.row==0)
    {
        cell.topLine.image=[UIImage imageNamed:@""];
    }
    NSInteger num=[model.P_state integerValue];
    //    NSInteger fristNum=[model1.P_state integerValue];
    //
    //    if (num==10000||num==2) {
    //        //当等于分配并且第一行为分配的时候
    //        if (fristNum==10000&&num==10000) {
    //
    //            if((model1.P_evaluated_start_time==nil||[model1.P_evaluated_start_time isEqualToString:@""])){
    //
    //            }else
    //            {
    //                cell.contentLab.text=[NSString stringWithFormat:@"预计处理：%@",model1.P_evaluated_start_time ];
    //
    //            }
    //
    //        }else
    //        {
    //            cell.contentLab.text=@"";
    //
    //        }
    //    }else
    if (num==2) {
        
        if((model1.P_evaluated_start_time==nil||[model1.P_evaluated_start_time isEqualToString:@""])){
            
        }else
        {
            cell.contentLab.text=[NSString stringWithFormat:@"预计处理：%@",model1.P_evaluated_start_time ];
            
        }
        
    }else
        
        //显示备注的分配、挂起、完成 转介
        if(num==1||num==5||num==8||num==3)
        {
            cell.contentLab.text= model.P_content;
            
            
        }else if(num==5||num==9)
        {
            cell.contentLab.text= model.P_content;
        }else if(num==20)//有采购单
        {
            cell.contentLab.text=@"采购单";
            
        }else if(num==21)
        {
            cell.stateLable.text=@"确认采购单";
            cell.contentLab.text=@"已确认";
            
        }else if(num==22)
        {
            cell.stateLable.text=@"确认采购单";
            cell.contentLab.text=@"已拒绝";
            
        }else
        {
            cell.contentLab.text= @"";
        }
    
    
    //进入采购单状态界面
    if (num==20) {
        [cell.contentLab bk_whenTapped:^{
            NSMutableDictionary *dic=[[NSMutableDictionary alloc]init];
            [dic setObject:model.P_general_uuid forKey:@"purchase_uuid"];
            [dic setObject:self.orderUuid forKey:@"warning_uuid"];
            
            if (self.gotoSampleState) {
                self.gotoSampleState(dic);
            }
        }];
    }else
    {
        
    }
    
    //报修显示电话，转介都显示电话
    if(num==0||num==3||num==30)
    {
        
        ProgressModel *firstModel = [self.mainData firstObject];
        if ([@[@"完成",@"评价", @"取消"] containsObject:firstModel.P_stateNm]) {
            
            cell.nameLab.text=[NSString stringWithFormat:@"%@  %@",model.P_name, @""];
            cell.phoneImg.image=[UIImage imageNamed:@""];
            
        }else
        {
            if (num==30) {
                if ([model.P_customer_uuid isEqualToString:@"admin"]) {//判断是否是后台录入
                    cell.nameLab.text=[NSString stringWithFormat:@"%@  %@",model.P_name, @""];
                    cell.phoneImg.image=[UIImage imageNamed:@""];
                }else
                {
                    cell.nameLab.text=[NSString stringWithFormat:@"%@",model.P_name];
                    UIImage*phoneImage=[UIImage imageNamed:@"Ico_home_detailPhone"];
                    cell.phoneImg.image=phoneImage;
                    
                    [cell.phoneView bk_whenTapped:^{
                        
                        //                        NSString *phone= [CClass replace:model.P_mobile];
                        [Common makePhoneWith:model.P_mobile];
                        
                        
                        
                    }];
                }
            }else
            {
                cell.nameLab.text=[NSString stringWithFormat:@"%@",model.P_name];
                UIImage*phoneImage=[UIImage imageNamed:@"Ico_home_detailPhone"];
                cell.phoneImg.image=phoneImage;
                
                [cell.phoneView bk_whenTapped:^{
                    
                    //                    NSString *phone= [CClass replace:model.P_mobile];
                    [Common makePhoneWith:model.P_mobile];
                    
                    
                    
                }];
            }
            
            
        }
        
    }else if(num==3)
    {
        cell.nameLab.text=[NSString stringWithFormat:@"%@",model.P_name];
    }else
    {
        cell.nameLab.text=@"";
        
    }
    if(num==9)
    {
        cell.contentLab.text= @"";
        cell.nameLab.text=@"";
        cell.timeLab.text=@"";
        cell.stateLable.text=@"";
    }else
    {
        
    }
    
    
    return cell;
    
    
}


@end
